
package genetic2;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import warehouse.Persistance;

/**
 *
 * @author Manolescu Razvan
 */

public class External {

  public static Map<String, Float> initFromFile(final String file){
      final Map<String, Float> map = new HashMap<String, Float>();
      BufferedReader brA = null ;
      try {
            brA = new BufferedReader( new FileReader( file ));
            String line = null;
            while((line = brA.readLine())!= null){
                String[] sp = line.split(" ");
                map.put(sp[0], Float.valueOf(sp[1]) / 100);
            }
            
        } catch (Exception ex) {
            System.out.println("[ERROR] Error reading probability file");
            ex.printStackTrace();
        }   
      
       return map;
    }
    
   public static Map<String, List<ColumnStatistics>> initColumnStatistics(final Map<String, Float> probMap, final Persistance p){
       final Map<String, List<ColumnStatistics>> map = new HashMap<String, List<ColumnStatistics>>();

       List<String> tableNames = p.getTablesFromDW();
       tableNames = filterTables(tableNames);
       
       for(String tableName : tableNames){ 
           List<ColumnStatistics> statistics = p.getTableStatistics(tableName);
           
           for(ColumnStatistics statistic : statistics){
               final Float prob = probMap.get("" + tableName + "." + statistic.name);
               statistic.probabilityOfUsage = prob == null ? GeneticParameters.initProb : prob;
           }
           map.put(tableName, statistics);
       }
       return map;
   }
   
   private static List<String> filterTables(final List<String> tables){
       final List<String> tableNames = new ArrayList<String>();
       for(String s : tables){
           if(!s.contains("datamart") && !s.contains("counters") && !s.contains("selectivity"))
           {
               tableNames.add(s);
           }
       }
       return tableNames;
   }
}
